package com.jdbctemplate.dao.impl;

import com.jdbctemplate.dao.StudentInfoDao;
import com.jdbctemplate.pojo.StudentInfo;
import com.springmvchomework.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 从卿
 * @create 2022/5/31-19:54
 */
@Repository
public class StudentInfoDaoImpl implements StudentInfoDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int  insertStudent(StudentInfo student) {
        String sql="insert into springmvc.student values (?,?,?)";
       Object args[]={student.getName(),student.getAge(),student.getAddress()};
       return jdbcTemplate.update(sql,args);
    }

    @Override
    public int updateStudent(StudentInfo student) {
        String sql="update springmvc.student set address=? where name=?";
        Object args[]={student.getAddress(),student.getName()};
        return  jdbcTemplate.update(sql,args);
    }

    @Override
    public StudentInfo findStuByName(String name) {
        return jdbcTemplate.queryForObject( "select * from springmvc.student where name=?",new BeanPropertyRowMapper<>(StudentInfo.class),name) ;
    }

    @Override
    public List<StudentInfo> selectStu() {
        List<StudentInfo>studentInfoList= jdbcTemplate.query( "select * from springmvc.student",new BeanPropertyRowMapper<StudentInfo>(StudentInfo.class)) ;
    return  studentInfoList;
    }
}
